package com.pachong.android.baseuicomponent.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.pachong.android.baseuicomponent.BaseComponentHelper;
import com.pachong.android.baseuicomponent.ILoadable;
import com.pachong.android.baseuicomponent.LoadState;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.view.BottomLoadStateView;

import java.util.List;

public abstract class RecyclerViewBaseFragment extends BaseFragment implements ILoadable,
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private static final String TAG = RecyclerViewBaseFragment.class.getSimpleName();
    private static final String KEY_ID = "key_id";

    private BaseComponentHelper mCompHelper;

    public RecyclerViewBaseFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        mCompHelper = new BaseComponentHelper(getActivity(), this);
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = createRecyclerContentView(inflater);
        if (getRecyclerView() == null) {
            throw new RuntimeException("RecyclerView is null: 覆盖createRecyclerContentView()， 并且调用setRecyclerView");
        }

        mCompHelper.initRecyclerView(getActivity(), this);
        setAdapter(createAdapter());

        if (getAdapter() != null) {
            getRecyclerView().setLayoutManager(createLayoutManager());
            mCompHelper.setHeaderView(getAdapter(), createHeaderView(inflater));
            setBottomLoadStateView(createBottomLoadStateView(inflater));
        }
        return view;
    }


    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        mCompHelper.addItemDecoration(itemDecoration);
    }


    public void setAdapter(RecyclerViewBaseAdapter adapter) {
        mCompHelper.setAdapter(adapter, this, this, this);
    }

    /**
     * 获取包含RecyclerView的容器，用户可以覆盖此方法，并且设置RecyclerView
     *
     * @param inflater
     * @return
     */
    public View createRecyclerContentView(LayoutInflater inflater) {
        return mCompHelper.createRecyclerContentView(inflater);
    }


    public void setRecyclerView(RecyclerView rv) {
        mCompHelper.setRecyclerView(rv);
    }


    public abstract LayoutManager createLayoutManager();


    public void startLoading() {
        mCompHelper.startLoading(getActivity(), this, this);
    }


    public RecyclerView getRecyclerView() {
        return mCompHelper.getRecyclerView();
    }


    public RecyclerViewBaseAdapter getAdapter() {
        return mCompHelper.getAdapter();
    }


    /**
     * 回调方法，创建adapter
     *
     * @return
     */
    public abstract RecyclerViewBaseAdapter createAdapter();


    public View createHeaderView(LayoutInflater inflater) {
        return null;
    }


    public BottomLoadStateView createBottomLoadStateView(LayoutInflater inflater) {
        return null;
    }


    public void setBottomLoadStateView(BottomLoadStateView stateView) {
        mCompHelper.setBottomLoadStateView(this, stateView);
    }

    /**
     * 回调方法，开始加载更多
     */
    public abstract void onStartLoading();

    /**
     * 改变加载状态
     *
     * @param state
     */
    public void changeLoadState(LoadState state) {
        mCompHelper.setLoadState(state);
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        return false;
    }

    public void clearDecorations() {
        mCompHelper.clearDecorations();
    }


    public boolean isEmpty() {
        return mCompHelper.isEmpty();
    }


    public List getData() {
        return getAdapter().getData();
    }

    /**
     * 设置触发自动加载的item的剩余数量
     *
     * @param itemCount
     */
    public void setTriggerLoadItemCount(int itemCount) {
        mCompHelper.setTriggerLoadItemCount(itemCount);
    }


    /**
     * 是否自动加载
     *
     * @param auto
     */
    public void setAutoLoading(boolean auto) {
        mCompHelper.setAutoLoading(auto);
    }


}
